package com.itany.corejava.day12;

import javax.swing.*;
import java.awt.*;

public class TestJMenuBar {
    private JFrame jf;

    private JMenuBar jmb;

    private JTextArea jta;

    public TestJMenuBar() {
        jf=new JFrame("记事本");
        jta=new JTextArea(20,50);

        jmb=new JMenuBar();

        String[] jmLab={"文件","编辑","查看","帮助"};
        String[][] jmiLab={
                {"新建","打开","","保存","另存为","","退出"},
                {"撤销","","剪切","复制","粘贴","","查找","替换"},
                {},
                {"关于记事本"}
        };


        for (int i = 0; i < jmLab.length; i++) {
            JMenu jm=new JMenu(jmLab[i]);

            if ("查看".equals(jmLab[i])){
                JMenu jm2=new JMenu("缩放");
                jm2.add(new JMenuItem("放大"));
                jm2.add(new JMenuItem("缩小"));
                jm.add(jm2);
                jmb.add(jm);
                continue;
            }

            for (int j = 0; j < jmiLab[i].length; j++) {
                if("".equals(jmiLab[i][j])){
                    jm.addSeparator();
                }else {
                    JMenuItem jmi = new JMenuItem(jmiLab[i][j]);
                    jm.add(jmi);
                }
            }

            jmb.add(jm);
        }
    }

    private void init(){
        jf.setJMenuBar(jmb);
        JScrollPane jsp=new JScrollPane(jta);
        jf.add(jsp);
    }

    public void show(){
        init();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestJMenuBar().show();
    }
}
